import Cookies from 'js-cookie'
import Config from '@/config'

const TokenKey = Config.TokenKey

export function getToken(tokenPar) {
  let token = Cookies.get(TokenKey)
  if (!token && tokenPar) {
    token = tokenPar
    setToken(token)
  }
  return token
}

export function setToken(token) {
  return Cookies.set(TokenKey, token, { expires: Config.tokenCookieExpires })
}

export function getCompanyId() {
  return Cookies.get('company_id')
}

export function setCompanyId(company_id) {
  return Cookies.set('company_id', company_id, { expires: Config.passCookieExpires })
}

export function setCompanyInfo(companyInfo) { return Cookies.set('company', companyInfo, { expires: Config.passCookieExpires }) }

export function getCompanyInfo() { return Cookies.get('company') }

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function addTokenParam(url) {
  return url + '?token=' + getToken() + '&company_id=' + getCompanyId()
}
